/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.poll.video.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.common.core.util.ListBeanUtils;
import com.hopes.bizz.poll.video.entity.PollVdVideoRegionEntity;
import com.hopes.bizz.poll.video.service.PollVdVideoRegionService;
import com.hopes.bizz.poll.video.vo.VdVideo;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.poll.video.entity.PollVdVideoEntity;
import com.hopes.bizz.poll.video.service.PollVdVideoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 视频监控管理
 *
 * @author cx
 * @date 2023-09-19 14:16:45
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/video")
@Tag(description = "video", name = "视频监控管理管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PollVdVideoController {

    private final PollVdVideoService pollVdVideoService;

    private final PollVdVideoRegionService pollVdVideoRegionService;


    /**
     * 分页查询
     *
     * @param page        分页对象
     * @param pollVdVideo 视频监控管理
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('video_video_view')")
    public R getPollVdVideoPage(Page page, PollVdVideoEntity pollVdVideo) {
        LambdaQueryWrapper<PollVdVideoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(pollVdVideo.getRegionId()), PollVdVideoEntity::getRegionId, pollVdVideo.getRegionId());
        wrapper.eq(ObjectUtil.isNotNull(pollVdVideo.getVideoType()), PollVdVideoEntity::getVideoType, pollVdVideo.getVideoType());
        wrapper.like(StrUtil.isNotBlank(pollVdVideo.getVideoName()), PollVdVideoEntity::getVideoName, pollVdVideo.getVideoName());
        wrapper.like(StrUtil.isNotBlank(pollVdVideo.getVideoCode()), PollVdVideoEntity::getVideoCode, pollVdVideo.getVideoCode());
        wrapper.eq(ObjectUtil.isNotNull(pollVdVideo.getIsUse()), PollVdVideoEntity::getIsUse, pollVdVideo.getIsUse());
        wrapper.eq(StrUtil.isNotBlank(pollVdVideo.getPlayMode()), PollVdVideoEntity::getPlayMode, pollVdVideo.getPlayMode());
        return R.ok(pollVdVideoService.page(page, wrapper));
    }


    /**
     * 列表查询
     *
     * @param pollVdVideo 视频监控管理
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('video_video_view')")
    public R getPollVdVideoList(PollVdVideoEntity pollVdVideo) {
        LambdaQueryWrapper<PollVdVideoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(pollVdVideo.getRegionId()), PollVdVideoEntity::getRegionId, pollVdVideo.getRegionId());
        wrapper.eq(ObjectUtil.isNotNull(pollVdVideo.getVideoType()), PollVdVideoEntity::getVideoType, pollVdVideo.getVideoType());
        wrapper.like(StrUtil.isNotBlank(pollVdVideo.getVideoName()), PollVdVideoEntity::getVideoName, pollVdVideo.getVideoName());
        wrapper.like(StrUtil.isNotBlank(pollVdVideo.getVideoCode()), PollVdVideoEntity::getVideoCode, pollVdVideo.getVideoCode());
        wrapper.eq(ObjectUtil.isNotNull(pollVdVideo.getIsUse()), PollVdVideoEntity::getIsUse, pollVdVideo.getIsUse());
        wrapper.eq(StrUtil.isNotBlank(pollVdVideo.getPlayMode()), PollVdVideoEntity::getPlayMode, pollVdVideo.getPlayMode());
        return R.ok(pollVdVideoService.list(wrapper));
    }

    /**
     * 查询树形区域和视频
     *
     * @param vdVideo 视频监控管理页面表单对象
     * @return
     */
    @ApiOperation("查询树形区域和视频")
    @PreAuthorize("@pms.hasPermission('video_video_view')")
    @GetMapping("/listRegionVideoTree")
    public R listRegionVideoTree(VdVideo vdVideo) {
        PollVdVideoRegionEntity videoRegion = new PollVdVideoRegionEntity();
        videoRegion.setId(Long.valueOf(0));
        JSONArray tree = pollVdVideoRegionService.listTree(videoRegion, true);
        return R.ok(tree);
    }


    /**
     * 通过id查询视频监控管理
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('video_video_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(pollVdVideoService.getById(id));
    }

    /**
     * 新增视频监控管理
     *
     * @param pollVdVideo 视频监控管理
     * @return R
     */
    @Operation(summary = "新增视频监控管理", description = "新增视频监控管理")
    @SysLog("新增视频监控管理")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('video_video_add')")
    public R save(@RequestBody PollVdVideoEntity pollVdVideo) {
        return R.ok(pollVdVideoService.save(pollVdVideo));
    }

    /**
     * 修改视频监控管理
     *
     * @param pollVdVideo 视频监控管理
     * @return R
     */
    @Operation(summary = "修改视频监控管理", description = "修改视频监控管理")
    @SysLog("修改视频监控管理")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('video_video_edit')")
    public R updateById(@RequestBody PollVdVideoEntity pollVdVideo) {
        return R.ok(pollVdVideoService.updateById(pollVdVideo));
    }

    /**
     * 通过id删除视频监控管理
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除视频监控管理", description = "通过id删除视频监控管理")
    @SysLog("通过id删除视频监控管理")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('video_video_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(pollVdVideoService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param pollVdVideo 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('video_video_export')")
    public List<PollVdVideoEntity> export(PollVdVideoEntity pollVdVideo) {
        return pollVdVideoService.list(Wrappers.query(pollVdVideo));
    }
}